// =============================================================================
//  STATS - Statistical Analysis Tools, v.0.01
//  Copyright (C) 2012  Marco Vettigli
// -----------------------------------------------------------------------------
//
// This program  is free software:  you can  redistribute it and/or  modify it
// under the terms of the  GNU General Public License as published by the Free
// Software Foundation,  either version 3 of the License,  or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY;  without even  the  implied  warranty  of  MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the  GNU General Public License  for
// more details.
//
// You  should  have received a copy of the  GNU General Public License  along
// with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// =============================================================================
//
// STABLEWIDGET.H
// The class STableWidget is the StatS user graphical interface to visualise
// and manage data of STable object.
//
// Author: Marco Vettigli
//
// =============================================================================

#ifndef STABLEWIDGET_H
#define STABLEWIDGET_H

// == include directives =======================================================

#include <QTableView>
#include <QTreeWidget>
#include "stable.h"

// == class definitions ========================================================

namespace StatS {

class STableWidget : public QWidget
{

    Q_OBJECT

public:

// == constructors and destructors =============================================

    // -- public ---------------------------------------------------------------
    // StatS::STableWidget::STableWidget(QWidget*, StatS::STable*)
    //
    // Default constructor for STableWidget class. It takes two argurmets:
    // parent QWidget pointer 'parent' and STable pointer 'table' to source
    // table. If 'table' is not provided, new STable object is created.
    //
    // -------------------------------------------------------------------------
    explicit STableWidget(QWidget* parent,
                          StatS::STable* table = 0);

// == getters ==================================================================

    // -- public ---------------------------------------------------------------
    // StatS::STableWidget::getTable()
    //
    // Returns a pointer to source STable.
    //
    // -------------------------------------------------------------------------
    StatS::STable* table() const;

    // -- public ---------------------------------------------------------------
    // StatS::STableWidget::name()
    //
    // Returns the name of source STable.
    //
    // -------------------------------------------------------------------------
    QString name() const;

// == setters ==================================================================

// == inspectors ===============================================================

// == accessor function declarations ===========================================

    // -- public ---------------------------------------------------------------
    // StatS::STableWidget::cutSelection()
    //
    // The function calls the cutting handler for currently selected data. Data
    // will be first copied to the clipboard, then cleared from the table. If
    // there is not a selection, the function will return.
    //
    // -------------------------------------------------------------------------
    void cutSelection();

    // -- public ---------------------------------------------------------------
    // StatS::STableWidget::copySelection()
    //
    // The function calls the copying handler for currently selected data. Data
    // will be first copied to the clipboard if the selection is not null, else
    // the function will return.
    //
    // -------------------------------------------------------------------------
    void copySelection();

    // -- public ---------------------------------------------------------------
    // StatS::STableWidget::copySelection()
    //
    // The function calls the clearing handler for currently selected data.
    // All the selected data in the table will be put null. If there is
    // no selection the function will return.
    //
    // -------------------------------------------------------------------------
    void clearSelection();

    // -- public ---------------------------------------------------------------
    // StatS::STableWidget::pasteClipboard()
    //
    // The function calls the pasting handler for currently selected data.
    // Data from the clipboard will be pasted on current selection. For more
    // information see private function on_cellsClearing().
    //
    // -------------------------------------------------------------------------
    void pasteClipboard();


    // -- public ---------------------------------------------------------------
    // StatS::STableWidget::updateSummaryTree()
    //
    // The function updates the table summary tree.
    //
    // -------------------------------------------------------------------------
    void updateSummaryTree();

    // -- public ---------------------------------------------------------------
    // StatS::STableWidget::updateSelection()
    //
    // The function updates table selection state.
    //
    // -------------------------------------------------------------------------
    void updateSelection();

    // -- public ---------------------------------------------------------------
    // StatS::STableWidget::keyPressEvent(QKeyEvent*)
    //
    // The function updates the key pressed state for handling complex
    // selection.
    //
    // -------------------------------------------------------------------------
    virtual void keyPressEvent(QKeyEvent* event);

    // -- public ---------------------------------------------------------------
    // StatS::STableWidget::keyReleasedEvent(QKeyEvent*)
    //
    // The function updates the key pressed state for handling complex
    // selection.
    //
    // -------------------------------------------------------------------------
    virtual void keyReleaseEvent(QKeyEvent* event);


// == variable declarations ====================================================

private:

    StatS::STable* _table;      // pointer to source STable
    Qt::Key _keyState;          // to store key pressed state
    int _colBuffer;             // to store starting column selection
    int _rowBuffer;             // to store starting row selection

    QTableView* tableView;      // main QTableView for displaying data
    QTreeWidget* tableSummary;  // QTreeWidget for displaying data structure

// == private function declarations ============================================

    // -- private --------------------------------------------------------------
    // StatS::STableWidget::setupUI()
    //
    // The function creates the user interface and connects signals.
    //
    // -------------------------------------------------------------------------
    void setupUI();

private slots:

    // -- private --------------------------------------------------------------
    // StatS::STableWidget::on_horizontalHeader_DoubleClick(int)
    //
    // The function handles double click event on column header.
    //
    // -------------------------------------------------------------------------
    void on_horizontalHeader_DoubleClick(int index);

    // -- private --------------------------------------------------------------
    // StatS::STableWidget::on_verticalHeader_DoubleClick(int)
    //
    // The function handles double click event on row header.
    //
    // -------------------------------------------------------------------------
    void on_verticalHeader_DoubleClick(int index);

    // -- private --------------------------------------------------------------
    // StatS::STableWidget::on_horizontalHeader_ContextMenu(QPoint);
    //
    // The function handles context menu request on column header.
    //
    // -------------------------------------------------------------------------
    void on_horizontalHeader_ContextMenu(QPoint point);

    // -- private --------------------------------------------------------------
    // StatS::STableWidget::on_verticalHeader_ContextMenu(QPoint);
    //
    // The function handles context menu request on row header.
    //
    // -------------------------------------------------------------------------
    void on_verticalHeader_ContextMenu(QPoint point);

    // -- private --------------------------------------------------------------
    // StatS::STableWidget::on_contextMenu(QPoint);
    //
    // The function handles context menu request on cells.
    //
    // -------------------------------------------------------------------------
    void on_contextMenu(QPoint point);

    // -- private --------------------------------------------------------------
    // StatS::STableWidget::on_columnInserting();
    //
    // The function handles column inserting command.
    //
    // -------------------------------------------------------------------------
    void on_columnInserting();

    // -- private --------------------------------------------------------------
    // StatS::STableWidget::on_columnsRemoving()
    //
    // The function handles column removing command.
    //
    // -------------------------------------------------------------------------
    void on_columnsRemoving();

    // -- private --------------------------------------------------------------
    // StatS::STableWidget::on_rowsInserting();
    //
    // The function handles row inserting command.
    //
    // -------------------------------------------------------------------------
    void on_rowsInserting();

    // -- private --------------------------------------------------------------
    // StatS::STableWidget::on_rowsRemoving();
    //
    // The function handles row removing command.
    //
    // -------------------------------------------------------------------------
    void on_rowsRemoving();

    // -- private --------------------------------------------------------------
    // StatS::STableWidget::on_columnPropertiesChanging();
    //
    // The function handles column properties changing command.
    //
    // -------------------------------------------------------------------------
    void on_columnPropertiesChanging();

    // -- private --------------------------------------------------------------
    // StatS::STableWidget::on_columnsConverting();
    //
    // The function handles column converting command.
    //
    // -------------------------------------------------------------------------
    void on_columnsConverting();

    // -- private --------------------------------------------------------------
    // StatS::STableWidget::on_rowColorChanging();
    //
    // The function handles row color changing command.
    //
    // -------------------------------------------------------------------------
    void on_rowColorChanging();

    // -- private --------------------------------------------------------------
    // StatS::STableWidget::on_rowMarkerChanging();
    //
    // The function handles row marker changing command.
    //
    // -------------------------------------------------------------------------
    void on_rowMarkerChanging();

    // -- private --------------------------------------------------------------
    // StatS::STableWidget::on_rowSizeChanging()
    //
    // The function handles row size changing command.
    //
    // -------------------------------------------------------------------------
    void on_rowSizeChanging();

    // -- private --------------------------------------------------------------
    // StatS::STableWidget::on_colSelecting(int)
    //
    // The function handles column selection.
    //
    // -------------------------------------------------------------------------
    void on_colSelecting(int col);

    // -- private --------------------------------------------------------------
    // StatS::STableWidget::on_rowSelecting(int)
    //
    // The function handles row selection.
    //
    // -------------------------------------------------------------------------
    void on_rowSelecting(int row);

    // -- private --------------------------------------------------------------
    // StatS::STableWidget::on_cellSelection(int)
    //
    // The function handles cell selection.
    //
    // -------------------------------------------------------------------------
    void on_cellSelection(QModelIndex index);

    // -- private --------------------------------------------------------------
    // StatS::STableWidget::on_headerDataChanged(Qt::Orientation, int, int)
    //
    // The function handles response to change of header data.
    //
    // -------------------------------------------------------------------------
    void on_headerDataChanged(Qt::Orientation orientation,
                              int section,
                              int role);       

    // -- private --------------------------------------------------------------
    // StatS::STableWidget::on_cellsCopying()
    //
    // The function handles response to cells copying request.
    //
    // -------------------------------------------------------------------------
    void on_cellsCopying();

    // -- private --------------------------------------------------------------
    // StatS::STableWidget::on_cellsPasting()
    //
    // The function handles response to cells pasting request.
    //
    // -------------------------------------------------------------------------
    void on_cellsPasting();

    // -- private --------------------------------------------------------------
    // StatS::STableWidget::on_cellsClearing()
    //
    // The function handles response to cells clearing request.
    //
    // -------------------------------------------------------------------------
    void on_cellsClearing();

    // -- private --------------------------------------------------------------
    // StatS::STableWidget::on_cellsCutting()
    //
    // The function handles response to cells cutting request.
    //
    // -------------------------------------------------------------------------
    void on_cellsCutting();

    // -- private --------------------------------------------------------------
    // StatS::STableWidget::on_tableSummaryItemClicked(QTreeWidgetItem*, int)
    //
    // The function handles response to click on QTreeWidget items.
    //
    // -------------------------------------------------------------------------
    void on_tableSummaryItemClicked(QTreeWidgetItem* item,
                                    int col);

};

}

#endif // STABLEWIDGET_H
